package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.AppletServer;
import com.ruoyi.fly.domain.vo.AppletServerSearchVo;
import com.ruoyi.fly.domain.vo.AppletServerVo;
import com.ruoyi.fly.mapper.AppletServerMapper;
import com.ruoyi.fly.mapper.ServerCategoryMapper;
import com.ruoyi.fly.service.AppletServerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppletServerServiceImpl implements AppletServerService {

    @Autowired
    private AppletServerMapper appletServerMapper;
    @Autowired
    private ServerCategoryMapper serverCategoryMapper;

    /**
     * 根据id查询服务
     *
     * @param id
     * @return
     */
    @Override
    public AppletServer selectAppletServerById(String id) {
        return appletServerMapper.selectAppletServerById(id);
    }

    /**
     * 查询服务列表
     *
     * @param appletServerSearchVo
     * @return
     */
    @Override
    public List<AppletServerVo> selectAppletServerList(AppletServerSearchVo appletServerSearchVo) {
        List<AppletServerVo> appletServerVos = appletServerMapper.selectAppletServerList(appletServerSearchVo);
        for (AppletServerVo appletServerVo : appletServerVos) {
            appletServerVo.setServerCategoryName(serverCategoryMapper.selectServerCategoryById(appletServerVo.getServerCategoryId()).getServerName());
        }
        return appletServerVos;
    }

    /**
     * 添加服务
     *
     * @param appletServer
     * @return
     */
    @Override
    public R insertAppletServer(AppletServer appletServer) {
        appletServer.setCreateUser(String.valueOf(SecurityUtils.getUserId()));
        return appletServerMapper.insertAppletServer(appletServer) > 0 ? R.ok("添加成功") : R.fail("操作失败");
    }

    /**
     * 修改服务
     *
     * @param appletServer
     * @return
     */
    @Override
    public R updateAppletServer(AppletServer appletServer) {
        appletServer.setUpdateUser(String.valueOf(SecurityUtils.getUserId()));
        return appletServerMapper.updateAppletServer(appletServer) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    /**
     * 上架服务
     *
     * @param id
     * @return
     */
    @Override
    public R onlineProduct(String id) {
        return appletServerMapper.onlineServerCategory(id) > 0 ? R.ok("上架成功") : R.fail("操作失败");
    }

    /**
     * 下架服务
     *
     * @param id
     * @return
     */
    @Override
    public R offlineProduct(String id) {
        return appletServerMapper.offlineServerCategory(id) > 0 ? R.ok("下架成功") : R.fail("操作失败");
    }

    /**
     * 删除服务
     *
     * @param id
     * @return
     */
    @Override
    public R deleteAppletServerById(String id) {
        return appletServerMapper.deleteAppletServerById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteAppletServerByIds(String[] ids) {
        return appletServerMapper.deleteAppletServerByIds(ids) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }
}
